Hệ Thống Quản Lý Du Lịch Trong Lập Trình C

1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<
string.h>
4
5 //Developed By Ankan Das

6
7 enum
state{menu,loggedin};
8 enum
state currentstate=menu;
9
10 typedef
struct user
11 {
12     
char username[100];
13     
char password[100];
14     
char place[100];
15     
float price;
16     
int numtick;
17     
struct user *next;
18 }user;

19
20 void
ShowBrochure();
21 user* InitializeList(user*);
22 user* AddUser(user*);

23 void
LoginUser(user*);
24 void
BookTicket(user*);
25 void
PrintTicket(user*);
26 void
CancelTicket(user*);
27 void
ChangePassword(user*);
28 void
LogoutUser();
29 void
CheckTicket(user*);
30 void
DisplayAll(user*);
31 void
WriteToFile(user*);
32 void
ExitProgram();
33
34 char
currentuser[100];
35
36 int
main()
37 {
38     printf(
"\t\t\t==== || TOURISM MANAGEMENT SYSTEM || ====\n");
39     user *h=NULL;
40     
int ch1,ch2;
41     h=InitializeList(h);
42     
while (1)
43     {
44         
45         
if(currentstate==menu)
46         {
47             
48             printf(
"\n\t\t\t\tAdd User - 1\n\t\t\t\tLogin User - 2\n\t\t\t\tBrochure - 3\n\t\t\t\tExit - 4\n\n\t\t\t\tEnter:");
49             scanf(
"%d",&ch1);
50             
switch(ch1)
51             {
52                 
case 1:
53                     h=AddUser(h);
54                     
break;
55                 
case 2:
56                     LoginUser(h);
57                     
break;
58                 
case 3:
59                     ShowBrochure();
60                     
break;
61                 
case 4:
62                     ExitProgram();
63                     exit(
0);
64                     
break;
65                 
default:
66                     printf(
"Not a valid input at this stage\n");
67             }
68         }
69         
else if(currentstate==loggedin)
70         {
71         system(
"CLS");
72         printf(
"\n\t\t\t\t=========================");
73         printf(
"\n\t\t\t\tTOURISM MANAGEMENT SYSTEM");
74         printf(
"\n\t\t\t\t=========================\n");
75             printf(
"\n\t\t\t\tBook Package - 1\n\t\t\t\tCheck Ticket - 2\n\t\t\t\tPrint Ticket - 3\n\t\t\t\tCancel Ticket - 4\n\t\t\t\tChange Password - 5"
76                    
"\n\t\t\t\tLogout User - 6\n\t\t\t\tBrochure - 7\n\t\t\t\tExit - 8\n");
77             scanf(
"%d",&ch2);
78             
switch(ch2)
79             {
80                 
case 1:
81                     BookTicket(h);
82                     system(
"PAUSE");
83                     system(
"CLS");
84                     
break;
85                 
case 2:
86                     CheckTicket(h);
87                     system(
"PAUSE");
88                     system(
"CLS");
89                     
break;
90                 
case 3:
91                     PrintTicket(h);
92                     system(
"PAUSE");
93                     system(
"CLS");
94                     
break;
95                 
case 4:
96                     CancelTicket(h);
97                     system(
"PAUSE");
98                     system(
"CLS");
99                     
break;
100                 
case 5:
101                     ChangePassword(h);
102                     system(
"PAUSE");
103                     system(
"CLS");
104                     
break;
105                 
case 6:
106                     LogoutUser(h);
107                     system(
"PAUSE");
108                     system(
"CLS");
109                     
break;
110                 
case 7:
111                     ShowBrochure();
112                     system(
"PAUSE");
113                     system(
"CLS");
114                     
break;
115                 
case 8:
116                     ExitProgram();
117                     exit(
0);
118                     
break;
119                 
default:
120                     printf(
"Not a valid input at this stage\n");
121             }
122         }
123     }
124     
return 0;
125 }
126
127 user* InitializeList(user *h)
128 {
129     user* t,*ptr,temp;
130     FILE *fp;
131     
int cc=0,x;
132     
float ff;
133     fp=fopen(
"users.txt","r");
134
135     
if(fp==NULL)
136         
return NULL;
137
138     
if(fgetc(fp)==EOF)
139         
return NULL;
140
141     rewind(fp);
142     
while(fscanf(fp,"%s %s %s %f %d",temp.username,temp.password,temp.place,&temp.price,&temp.numtick)!=EOF)
143     {
144         ptr=(user*)malloc(
sizeof(user));
145         strcpy(ptr->username,temp.username);
146         strcpy(ptr->password,temp.password);
147         strcpy(ptr->place,temp.place);
148         ptr->price=temp.price;
149         ptr->numtick=temp.numtick;
150         ptr->next=NULL;
151
152         
if(h==NULL)
153             h=t=ptr;
154         
else
155         {
156             h->next=ptr;
157             h=ptr;
158         }
159     }
160     fclose(fp);
161     
return t;
162 }

163
164 void
WriteToFile(user *h)
165 {
166     FILE *fp;
167     fp=fopen(
"users.txt","w");
168     
while(h!=NULL)
169     {
170         fprintf(fp,
"%s %s %s %f %d\n",h->username,h->password,h->place,h->price,h->numtick);
171         h=h->next;
172     }
173     fclose(fp);
174 }

175
176 void
ShowBrochure()
177 {
178     system(
"CLS");
179     printf(
"\tPRICE LIST\n=============================\n1. LL - Antelope Canyon Tours - Rs 40000\n2. JK - Grand Canyon Local Tours - Rs 60000\n3. SK - San Francisco Local Tours - Rs 25000\n4. SHM - Miami Vacation - Rs 38000\n"
180            
"5. AND - Hawaii - Rs 120000\n6. BHB - Atlanta Vacation - Rs 10000\n7. AG - San Francisco - Rs 30000\n8. ND - Alaska Vacation - Rs 32000\n9. RJ - Orlando Vacation - Rs 45000\n10. SI - South US Tour - Rs 250000\n");
181 }

182
183 void
CheckTicket(user *h)
184 {
185     
while(h!=NULL)
186     {
187         
if(!strcmp(h->username,currentuser))
188             
break;
189         h=h->next;
190     }
191     
if(!strcmp(h->place,"\0") || h->price==0.0 || h->numtick==0)
192     {
193         printf(
"You do not have a ticket booked yet\n");
194         
return;
195     }
196     
float total=0.0;
197     total=(h->price)*(h->numtick);
198     printf(
"You have booked %d tickets for a sum total of Rs %f for tour code %s\n",h->numtick,total,h->place);
199 }
200
201 user* AddUser(user* h)
202 {
203     user *t;
204     t=h;
205     user *nw;
206     nw=(user*)malloc(
sizeof(user));
207     fflush(stdin);
208     printf(
"Enter username or email\n");
209     scanf(
"%s",nw->username);
210     
while(h!=NULL)
211     {
212         
if(!strcmp(h->username,nw->username))
213         {
214             printf(
"That email already exists\n");
215             
return t;
216         }
217         h=h->next;
218     }
219     h=t;
220     fflush(stdin);
221     printf(
"Enter password\n");
222     scanf(
" %[^\n]s",&nw->password);
223     nw->next=NULL;
224     strcpy(nw->place,
"N/A");
225     nw->price=
0.0;
226     nw->numtick=
0;
227
228     
if(h==NULL)
229     {
230         h=t=nw;
231     }
232     
else
233     {
234         
while(h->next!=NULL)
235         {
236             h=h->next;
237         }
238         h->next=nw;
239     }
240     WriteToFile(t);
241     
return t;
242 }

243
244 void
LoginUser(user* h)
245 {
246     
char username[100];
247     
char password[100];
248     fflush(stdin);
249     printf(
"\n\n");
250     printf(
"\t\tEnter Email/Username:\n\t\t");
251     scanf(
"%s",username);
252     fflush(stdin);
253     printf(
"\n\t\tEnter Password:\n\t\t");
254     scanf(
" %[^\n]s",password);
255     
while(h!=NULL)
256     {
257         
if((!strcmp(h->username,username)) && (!strcmp(h->password,password)))
258         {
259             currentstate=loggedin;
260             strcpy(currentuser,username);
261             
262             printf(
"\n\t\tLogin successful!\n");
263             system(
"PAUSE");
264             
return;
265         }
266         
else if((!strcmp(h->username,username)) && (strcmp(h->password,password)))
267         {
268             printf(
"Password mismatch\n");
269             
return;
270         }
271         h=h->next;
272     }
273     printf(
"Sorry, no such user record was found\n");
274 }

275
276 void
BookTicket(user *h)
277 {
278     user *t=h;
279     
char place[100];
280     
while(h!=NULL)
281     {
282         
if(!strcmp(h->username,currentuser))
283             
break;
284         h=h->next;
285     }
286     
if(h==NULL)
287         
return;
288     
if(h->price!=0.0)
289     {
290         printf(
"You must cancel your previous ticket before buying a new one\n");
291         
return;
292     }
293     ShowBrochure();
294     
float pricelist[]={40000.0,60000.0,25000.0,38000.0,120000.0,10000.0,30000.0,32000.0,45000.0,250000.0};
295     fflush(stdin);
296     printf(
"\nEnter place code (eg: LL, JK)\n");
297     scanf(
" %[^\n]s",place);
298     
char choice;
299     fflush(stdin);
300     printf(
"\nWould You Like to Confirm Booking?\n[1] - Yes\n[2] - No\n");
301     scanf(
"%c",&choice);
302     
float price;
303     
if(choice!='1')
304         
return;
305     
if(strcmp(place,"LL")==0)
306         price=pricelist[
0];
307     
else if(strcmp(place,"JK")==0)
308         price=pricelist[
1];
309     
else if(strcmp(place,"SK")==0)
310         price=pricelist[
2];
311     
else if(strcmp(place,"SHM")==0)
312         price=pricelist[
3];
313     
else if(strcmp(place,"AND")==0)
314         price=pricelist[
4];
315     
else if(strcmp(place,"BHB")==0)
316         price=pricelist[
5];
317     
else if(strcmp(place,"AG")==0)
318         price=pricelist[
6];
319     
else if(strcmp(place,"ND")==0)
320         price=pricelist[
7];
321     
else if(strcmp(place,"RJ")==0)
322         price=pricelist[
8];
323     
else if(strcmp(place,"SI")==0)
324         price=pricelist[
9];
325     
else
326     {
327         printf(
"That tour code doesn't exist\n");
328         
return;
329     }
330     printf(
"Enter the number of tickets you want to book?\n");
331     scanf(
"%d",&h->numtick);
332     
if(h->numtick==0)
333         
return;
334     strcpy(h->place,place);
335     h->price=price;
336     WriteToFile(t);
337     printf(
"Bookings Done!!\n");
338     system(
"PAUSE");
339     
340 }

341
342 void
PrintTicket(user *h)
343 {
344     
while(h!=NULL)
345     {
346         
if(!strcmp(h->username,currentuser))
347             
break;
348         h=h->next;
349     }
350     
if(!strcmp(h->place,"\0") || h->price==0.0 || h->numtick==0)
351     {
352         printf(
"You do not have a ticket booked yet\n");
353         
return;
354     }
355     
float total=0.0;
356     total=(h->price)*(h->numtick);
357     FILE *fp;
358     
char filename[50];
359     strcpy(filename,h->username);
360     strcat(filename,
"_ticket.txt");
361     fp=fopen(filename,
"w");
362     
if(fp==NULL)
363     {
364         printf(
"Problem opening the file\n");
365         
return;
366     }
367     
if(fgetc(fp)==EOF)
368     {
369         fprintf(fp,
"TOURISM TICKET\n===============\n\n");
370     }
371     fprintf(fp,
"Email ID: %s\nTour Code: %s\nTicket Cost: Rs %f\nNumber of tickets: %d\nTotal Cost: Rs %f\n",h->username,h->place,h->price,h->numtick,total);
372     fclose(fp);
373 }

374 //TMS1353f

375 void
CancelTicket(user *h)
376 {
377     user *t=h;
378     
while(h!=NULL)
379     {
380         
if(!strcmp(h->username,currentuser))
381             
break;
382         h=h->next;
383     }
384
385     
int flag=-1;
386
387     
if(h==NULL)
388         printf(
"No such user\n");
389
390     
if(strcmp(h->place,"LL")==0)
391         flag++;
392     
else if(strcmp(h->place,"JK")==0)
393         flag++;
394     
else if(strcmp(h->place,"SK")==0)
395         flag++;
396     
else if(strcmp(h->place,"SHM")==0)
397         flag++;
398     
else if(strcmp(h->place,"AND")==0)
399         flag++;
400     
else if(strcmp(h->place,"BHB")==0)
401         flag++;
402     
else if(strcmp(h->place,"AG")==0)
403         flag++;
404     
else if(strcmp(h->place,"ND")==0)
405         flag++;
406     
else if(strcmp(h->place,"RJ")==0)
407         flag++;
408     
else if(strcmp(h->place,"SI")==0)
409         flag++;
410     
else
411     {
412         printf(
"You haven't booked a tour yet\n");
413         
return;
414     }
415     
if(flag==0)
416     {
417         printf(
"Your ticket has been successfully cancelled\nA refund of Rs %f for Tour Code %s for %d tickets will soon be made to your original source of purchase\n",h->price,h->place,h->numtick);
418         strcpy(h->place,
"N/A");
419         h->price=
0.0;
420         h->numtick=
0;
421         WriteToFile(t);
422     }
423 }

424
425 void
ChangePassword(user *h)
426 {
427     user *t=h;
428     
char passcurr[100];
429     fflush(stdin);
430     printf(
"Enter your current password to continue:\n");
431     scanf(
" %[^\n]s",passcurr);
432     
while(h!=NULL)
433     {
434         
if(!strcmp(h->username,currentuser))
435             
break;
436         h=h->next;
437     }
438     
if(h==NULL)
439         
return;
440     
if(!strcmp(passcurr,h->password))
441     {
442         printf(
"Enter new password:\n");
443         scanf(
" %[^\n]s",h->password);
444     }
445     WriteToFile(t);
446 }

447
448 void
LogoutUser()
449 {
450     
if(currentstate==menu || strcmp(currentuser,"\0")==0)
451     {
452         printf(
"You must be logged in to logout\n");
453         
return;
454     }
455     strcpy(currentuser,
"\0");
456     currentstate=menu;
457     printf(
"You have been successfully logged out\n");
458 }

459
460 void
ExitProgram()
461 {
462     printf(
"Exiting...\nDeveloped By Ankan Das, Kolkata\n\nBrought To You By code-projects.org\n\nPress \"Enter/Return\" to exit");
463     
char exitprog;
464     fflush(stdin);
465     scanf(
"%c",&exitprog);
466 }


Gõ tìm kiếm nhanh...